package com.liziba.trend.controller;

import com.liziba.trend.api.Index;
import com.liziba.trend.entity.Result;
import com.liziba.trend.entity.StatusCode;
import com.liziba.trend.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @ProjectName: trend-parent
 * @Package: com.liziba.trend.controller
 * @ClassName: IndexController
 * @Author: liziba
 * @Description: 获取基金代码数据控制器
 * @Date: 2021/2/1 16:54
 * @Version: 1.0
 */

@RestController
@RequestMapping("/index")
@CrossOrigin
public class IndexController {

    /**
     * 获取基金代码数据服务
     */
    @Autowired
    private IndexService indexService;

    /**
     * 接口获取基金代码
     * @return
     */
    @GetMapping("/fetch")
    public Result<List<Index>> fetch() {
        List<Index> indexList = indexService.fetchIndexesFromThirdPart();
        return new Result<List<Index>>(true, StatusCode.OK, "接口获取数据成功！", indexList);
    }

    /**
     * 刷新缓存
     * @return
     * @throws Exception
     */
    @GetMapping("/fresh")
    public Result<List<Index>> fresh() throws Exception {
        List<Index> indexList = indexService.fresh();
        return new Result<List<Index>>(true, StatusCode.OK, "刷新缓存成功！", indexList);
    }

    /**
     * 获取缓存
     * @return
     * @throws Exception
     */
    @GetMapping("/get")
    public Result<List<Index>> get() throws Exception {
        List<Index> indexList = indexService.get();
        return new Result<List<Index>>(true, StatusCode.OK, "获取缓存中数据成功！", indexList);
    }

    /**
     * 移除缓存
     * @return
     * @throws Exception
     */
    @GetMapping("/remove")
    public Result remover() throws Exception {
        indexService.remove();
        return new Result(true, StatusCode.OK, "移除缓存！", null);
    }

}
